﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace RoadTeam.FineFile
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            MessageBox.Show("打包目录:F:\\乐天打包\\2013062802\n输出目录:C:\\Users\\ZKQ\\Desktop\\新建文件夹 (4)\ntxt文件的话就这样:\nmodules\\FM\\Charge\\Charge.aspx\nmodules\\FM\\Charge\\FrmAddEAccountFee.aspx\nmodules\\CS\\Maintenance\\FrmAddProject.aspx");
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(textBox1.Text) || string.IsNullOrEmpty(textBox2.Text) || string.IsNullOrEmpty(textBox3.Text))
            {
                MessageBox.Show("三个文本框都不允许为空");
            }
            else if (!Directory.Exists(textBox1.Text) || !Directory.Exists(textBox2.Text))
            {
                MessageBox.Show("指定的文件夹不存在");
            }
            else
            {
                GetFile(textBox1.Text, textBox2.Text, textBox3.Text);
            }
        }

        private List<String> GetPathList(String Path)
        {

            StreamReader reader = new StreamReader(Path);
            List<string> li = new List<string>();
            while (!reader.EndOfStream)
            {
                li.Add(reader.ReadLine().Replace('/', '\\'));
            }
            return li;
        }

        #region 复制文件
        private void GetFile(string SourceDir, string OutDir, string TxTPath)
        {
            try
            {
                List<string> FileListInfo = new List<string>();
                //OpenFileDialog op = new OpenFileDialog();
                //op.InitialDirectory = @"c:\";
                //op.RestoreDirectory = true;
                //op.Filter = "文本文件(*.txt)|*.txt|所有文件(*.*)|*.*";
                //if (op.ShowDialog() == DialogResult.OK)
                // {
                //   string aa = op.FileName;

                progressBar1.Value = 0;
                progressBar1.Maximum = GetPathList(TxTPath).Count;
                progressBar1.Step = 1;


                StreamReader sr = new StreamReader(TxTPath);
                string duqu;
                duqu = sr.ReadLine();
                do
                {
                    FileListInfo.Add(duqu);
                    //复制文件
                    CopyFile(SourceDir, OutDir, duqu);
                    duqu = sr.ReadLine();

                    progressBar1.Value += progressBar1.Step;
                } while (duqu != null && duqu != "");
                //textBox1.Text = duqu;
                GetDirectorFile(SourceDir, OutDir, OutDir);
                //}
                // MessageBox.Show("文件获取成功");
            }
            catch (Exception ex)
            {
                MessageBox.Show("出错了,错误信息:" + ex.Message);
                throw;
            }
        }

        private void CopyFile(string SourceDir, string OutDir, string FileDirAndName)
        {
            //把路径标准化,把/替换为\
            FileDirAndName = FileDirAndName.Replace("/", "\\");
            List<string> DirNameArr = FileDirAndName.Split('\\').ToList();
            if (DirNameArr != null || DirNameArr.Count > 0)
            {
                for (int i = 0; i < DirNameArr.Count; i++)
                {
                    string Item = DirNameArr[i];
                    if (!string.IsNullOrEmpty(Item))
                    {
                        //判断是不是最后一个文件名
                        if (i == (DirNameArr.Count - 1))
                        {   //正式复制文件
                            CopyFileToTarget(SourceDir, OutDir, Item);
                        }
                        //如果是文件夹
                        else if (!Directory.Exists(OutDir + "\\" + Item) && Directory.Exists(SourceDir + "\\" + Item))
                        {
                            Directory.CreateDirectory(OutDir + "\\" + Item);
                        }
                        SourceDir += "\\" + Item;
                        OutDir += "\\" + Item;

                    }
                }
            }
        }

        private void CopyFileToTarget(String source, String destination, string CopyFileName)
        {
            try
            {

                DirectoryInfo info = new DirectoryInfo(source);
                FileSystemInfo[] sysInfos = info.GetFileSystemInfos();

                foreach (FileSystemInfo fsi in info.GetFileSystemInfos())
                {
                    string destName = Path.Combine(destination, fsi.Name);
                    if (fsi is System.IO.FileInfo)
                    {
                        string sourceName = Path.Combine(source, fsi.Name);
                        try
                        {
                            //just in case the destination is readonly
                            if (File.Exists(destName))
                            {
                                new FileInfo(destName).IsReadOnly = false;
                            }
                            if (fsi.Name == CopyFileName)
                            {
                                File.Copy(fsi.FullName, destName, true);
                            }
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show("出错了,错误信息:" + ex.Message);
                            return;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("出错了,错误信息:" + ex.Message);
                return;
            }
        }
        #endregion

        #region 复制bin文件
        private void GetDirectorFile(string sourceDir, string TarDir, string Dir)
        {
            DirectoryInfo info = new DirectoryInfo(Dir);
            FileInfo[] FileInfos = info.GetFiles();
            if (FileInfos.Count() > 0)
            {
                foreach (FileInfo File in FileInfos)
                {
                    string binName = getAspxHead(File.FullName);
                    if (!Directory.Exists(TarDir + "\\bin"))
                    {
                        Directory.CreateDirectory(TarDir + "\\bin");
                    }
                    CopyFileToTarget(sourceDir + "\\bin", TarDir + "\\bin", GetBinFileName(binName) + ".dll");
                }
            }
            DirectoryInfo[] DirInfos = info.GetDirectories();
            if (DirInfos.Count() > 0)
            {
                foreach (DirectoryInfo Dirinfo in DirInfos)
                {
                    GetDirectorFile(sourceDir, TarDir, Dirinfo.FullName);
                }
            }
        }

        private string getAspxHead(string FullDir)
        {
            FileStream fs = new FileStream(FullDir, FileMode.Open);

            StreamReader m_streamReader = new StreamReader(fs);

            m_streamReader.BaseStream.Seek(0, SeekOrigin.Begin);
            //string arry = "";
            string strLine = m_streamReader.ReadLine();
            //do
            //{
            //    string[] split = strLine.Split('=');
            //    string a = split[0];
            //    if (a.ToLower() == "ip")
            //    {
            //        arry += strLine + "\n";

            //    }
            //    strLine = m_streamReader.ReadLine();

            //} while (strLine != null && strLine != "");

            m_streamReader.Close();
            m_streamReader.Dispose();
            fs.Close();
            fs.Dispose();
            return strLine;
        }

        private string GetBinFileName(string AspxHead)
        {
            string result = "";
            string[] Arr = AspxHead.Split('"');
            foreach (string item in Arr)
            {
                if (item.Contains(","))
                {
                    result = item.Split(',')[1].Trim();
                }
            }
            return result;
        }
        #endregion

        private void button2_Click_1(object sender, EventArgs e)
        {
            FolderBrowserDialog fd1 = new FolderBrowserDialog();
            if (fd1.ShowDialog() == DialogResult.OK)
            {
                textBox1.Text = fd1.SelectedPath;
            }

        }

        private void button3_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog fd1 = new FolderBrowserDialog();
            if (fd1.ShowDialog() == DialogResult.OK)
            {
                textBox2.Text = fd1.SelectedPath;
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            OpenFileDialog op = new OpenFileDialog();
            op.Filter = "*.txt|";
            if (op.ShowDialog() == DialogResult.OK)
            {
                textBox3.Text = op.FileName;
            }
        }


    }
}
